package com.mit.wx.wx.dto;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;

public class CustomerServiceMessage extends HashMap<String, Object> {
    private static final String TO_USER_KEY = "touser";
    private static final String MSG_TYPE_KEY = "msgtype";
    private static final String TEXT_MSG_KEY = "text";
    private static final String IMAGE_MSG_KEY = "image";
    private static final String VOICE_MSG_KEY = "voice";
    private static final String VIDEO_MSG_KEY = "video";
    private static final String MUSIC_MSG_KEY = "music";
    private static final String MPNEWS_MSG_KEY = "mpnews";
    private static final String NEWS_MSG_KEY = "articles";
    private static final String WXCARD_MSG_KEY = "wxcard";
    private static final String MINIPROGRAMPAGE_MSG_KEY = "miniprogrampage";
    private static final String CUSTOMSERVICE_MSG_KEY = "customservice";

    public CustomerServiceMessage toUser(String toUserOpenId) {
        put(TO_USER_KEY, toUserOpenId);
        return this;
    }

    public CustomerServiceMessage msgType(String msgType) {
        put(MSG_TYPE_KEY, msgType);
        return this;
    }

    public CustomerServiceMessage customerService(String kfAccount) {
        put(CUSTOMSERVICE_MSG_KEY, new KfAccount(kfAccount));
        return this;
    }

    public CustomerServiceMessage text(String content) {
        put(MSG_TYPE_KEY, "text");
        put(TEXT_MSG_KEY, new TextMessage(content));
        return this;
    }

    public CustomerServiceMessage image(String mediaId) {
        put(MSG_TYPE_KEY, "image");
        put(IMAGE_MSG_KEY, new MediaMessage(mediaId));
        return this;
    }

    public CustomerServiceMessage voice(String mediaId) {
        put(MSG_TYPE_KEY, "voice");
        put(VOICE_MSG_KEY, new MediaMessage(mediaId));
        return this;
    }

    public CustomerServiceMessage video(String mediaId, String thumbMediaId, String title, String describe) {
        put(MSG_TYPE_KEY, "video");
        VideoMessage message = new VideoMessage(mediaId);
        message.setThumbMediaId(thumbMediaId);
        message.setThumbMediaId(title);
        message.setDescription(describe);
        put(VIDEO_MSG_KEY, message);
        return this;
    }

    public CustomerServiceMessage music(String title, String description, String musicUrl, String hqMusicUrl, String thumbMediaId) {
        put(MSG_TYPE_KEY, "music");
        MusicMessage message = new MusicMessage();
        message.setTitle(title);
        message.setDescription(description);
        message.setMusicUrl(musicUrl);
        message.setHqMusicUrl(hqMusicUrl);
        message.setThumbMediaId(thumbMediaId);
        put(MUSIC_MSG_KEY, message);
        return this;
    }

    public CustomerServiceMessage news(String title, String description, String url, String picUrl) {
        put(MSG_TYPE_KEY, "news");
        NewsMessage message = new NewsMessage();
        message.setTitle(title);
        message.setDescription(description);
        message.setUrl(url);
        message.setPicUrl(picUrl);
        put(NEWS_MSG_KEY, message);
        return this;
    }

    public CustomerServiceMessage mpNews(String mediaId) {
        put(MSG_TYPE_KEY, "mpnews");
        put(MPNEWS_MSG_KEY, new MediaMessage(mediaId));
        return this;
    }

    public CustomerServiceMessage wxCard(String cardId) {
        put(MSG_TYPE_KEY, "wxcard");
        put(WXCARD_MSG_KEY, new WxCardMessage(cardId));
        return this;
    }

    public CustomerServiceMessage miniProgramPage(String title,
                                                  String appId,
                                                  String pagePath,
                                                  String thumbMediaId) {
        put(MSG_TYPE_KEY, "miniprogrampage");
        MiniProgramPageMessage message = new MiniProgramPageMessage();
        message.setTitle(title);
        message.setAppId(appId);
        message.setPagePath(pagePath);
        message.setThumbMediaId(thumbMediaId);
        put(MINIPROGRAMPAGE_MSG_KEY, message);
        return this;
    }

}

@Data
@AllArgsConstructor
class KfAccount {
    @JSONField(name = "kf_account")
    private String kfAccount;
}

@Data
@AllArgsConstructor
class TextMessage {
    private String content;
}

@Data
@AllArgsConstructor
class MediaMessage {
    @JSONField(name = "media_id")
    private String mediaId;
}

@EqualsAndHashCode(callSuper = true)
@Data
class VideoMessage extends MediaMessage {
    @JSONField(name = "thumb_media_id")
    private String thumbMediaId;
    private String title;
    private String description;

    VideoMessage() {
        super(null);
    }

    VideoMessage(String mediaId) {
        super(mediaId);
    }

}

@Data
class MusicMessage {
    private String title;
    private String description;
    @JSONField(name = "musicurl")
    private String musicUrl;
    @JSONField(name = "hqmusicurl")
    private String hqMusicUrl;
    @JSONField(name = "thumb_media_id")
    private String thumbMediaId;
}

@Data
class NewsMessage {
    private String title;
    private String description;
    private String url;
    @JSONField(name = "picurl")
    private String picUrl;
}

@Data
@AllArgsConstructor
class WxCardMessage {
    @JSONField(name = "card_id")
    private String cardId;
}

@Data
class MiniProgramPageMessage {
    private String title;
    @JSONField(name = "appid")
    private String appId;
    @JSONField(name = "pagepath")
    private String pagePath;
    @JSONField(name = "thumb_media_id")
    private String thumbMediaId;
}